//
//  PreferentialViewController.m
//  classroom
//
//  Created by apple on 12-10-23.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "PreferentialViewController.h"
#import "JSONKit.h"
#import "PreferentialTableViewCell.h"
#import "ASIHTTPRequest.h"
#import "UrlData.h"
#import "CourseInformationViewController.h"

@interface PreferentialViewController ()

@end

@implementation PreferentialViewController
@synthesize PreferentialTableView;
@synthesize segment;
@synthesize preferenceDic;//活动字典
@synthesize discountDic;//折扣字典
@synthesize selectedSegment;
@synthesize nameStrArray;///存放活动折扣的名字
@synthesize dateStrArray;//存放活动折扣的时间
@synthesize temperatureStrArray,cityStrArray,weatherDic;//测试天气预报 json用的array

@synthesize PrefRequestResultDic; //存放url请求返回的数据
@synthesize informationArray;//将返回的数据解析，获取字典，并存入数组

@synthesize loadinglabel;
@synthesize waitIndicatorView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title=@"优惠活动";
            }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.rightBarButtonItem=[[[UIBarButtonItem alloc]initWithTitle:@"主页" style:UIBarButtonItemStylePlain target:self action:@selector(BackTOMenu)]autorelease];

    NSString* urlString=[NSString stringWithFormat:@"%@%@",ClassRoom_URL,ACTIVE_URL];
    [self requestData:urlString];
    
    
    //初始化 tableview
    PreferentialTableView=[[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 416) style:UITableViewStylePlain]autorelease];
    [PreferentialTableView setDelegate:self];
    [PreferentialTableView setDataSource:self];
    [self.view addSubview:PreferentialTableView];
    
    [self displayActiveIndicatorView];//显示等待ActiveIndicatorView
    //初始化请求数据
}

-(void)viewWillAppear:(BOOL)animated
{

    [PreferentialTableView reloadData];
}

- (void)viewDidUnload
{
    [self setSegment:nil];
    [self setPreferentialTableView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}



-(void)BackTOMenu//在二级菜单下返回主页
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    [_dataArray release];
    
    [segment release];
    [PreferentialTableView release];
    [super dealloc];
}

-(void)setCellText:(NSMutableDictionary *)tempDic
{
    
}

-(void)requestData:(NSString *)urlString
{
    NSURL *url=[NSURL URLWithString:urlString];
    //NSError *error;
    //NSString *jsonString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    
    [request setDelegate:self];
    
    [request startAsynchronous];

//    _dataArray=[[jsonString objectFromJSONString] retain];
//    NSLog(@"result count = %d",[_dataArray count]);
//    NSLog(@"result%@",[_dataArray objectAtIndex:1]);
//    NSLog(@"qqqqqqqq%@",[[[_dataArray objectAtIndex:1] objectForKey:@"information"]objectForKey:@"classKind"]);
}


-(void)parseDate:(NSMutableDictionary*)dic
{
    [cityStrArray addObject:[dic objectForKey:@"city"]];
    [temperatureStrArray addObject:[dic objectForKey:@"weather1"]];
    
    
}


//开始显示 UIActivityIndicatorView
- (void) displayActiveIndicatorView 
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
    if (waitIndicatorView == nil) {
        waitIndicatorView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(140, 50, 46, 46)];
        loadinglabel=[[UILabel alloc]initWithFrame:CGRectMake(140, 100, 80, 30)];
        loadinglabel.text=@"loading...";
        loadinglabel.BackgroundColor=[UIColor clearColor];
        [waitIndicatorView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
        [waitIndicatorView startAnimating];
        
        [self.view addSubview:waitIndicatorView];
        [self.view addSubview:loadinglabel];
    }
    
    [pool release];
}
//结束显示 UIActivityIndicatorView
- (void) dismissActiveIndicatorView
{
    if (waitIndicatorView)
    {
        [waitIndicatorView stopAnimating];
        [waitIndicatorView removeFromSuperview];
        [waitIndicatorView release]; waitIndicatorView = nil;
        [loadinglabel removeFromSuperview];
        [loadinglabel release];
        loadinglabel=nil;
    }
}

#pragma mark -

#pragma mark ASIHTTPRequest

- (void)requestFinished:(ASIHTTPRequest *)request

{
    
    // 当以文本形式读取返回内容时用这个方法
    
    NSString *responseString = [request responseString];
    _dataArray=[[responseString objectFromJSONString] retain];
//    NSLog(@"result count = %d",[_dataArray count]);
//    NSLog(@"result%@",[_dataArray objectAtIndex:1]);
//    NSLog(@"qqqqqqqq%@",[[[_dataArray objectAtIndex:1] objectForKey:@"information"]objectForKey:@"classKind"]);
//    NSLog(@"%@",responseString);
    [self dismissActiveIndicatorView];//结束显示ActiveIndicatorView
    [PreferentialTableView reloadData];
    // NSData *responseData = [request responseData];
    
}

- (void)requestFailed:(ASIHTTPRequest *)request

{
    
    NSError *error = [request error];
    NSLog(@"%@",error);
    
}

#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_dataArray count];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    PreferentialTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if(cell == nil)
    {
        cell = [[[PreferentialTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    NSLog(@"_dataArray == %d",[_dataArray count]);

    [cell setCellData:(NSDictionary *)[_dataArray objectAtIndex:indexPath.row]];
    return cell;
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CourseInformationViewController *courseInfoViewController=[[[CourseInformationViewController alloc]init] autorelease];
    [self.navigationController pushViewController:courseInfoViewController animated:YES];
    [courseInfoViewController SetSomeChanges:(NSDictionary *)[[_dataArray objectAtIndex:indexPath.row] objectForKey:@"information"]buttonEnable:NO];
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}




@end
